import axios from 'axios'
import auth from '../utils/auth'
import {Notification} from  'element-ui'

// axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
 
export function request(config) {
    const instance = axios.create({
        // baseURL: '/api',    
        baseURL:'http://101.200.161.31:7003/hin',
        timeout: 5000,
        headers: {
            // 'Content-Type': 'multipart/form-data',
          "Content-Type": "application/json"
        }
        
    })

    instance.interceptors.request.use(config => {
        config.headers.Authorization = auth.getAuthorization()
        return config
    }) 
    instance.interceptors.response.use(res => {
        let {code, data,msg} = res.data
        // let {status, data,msg} = res
        if(code == '0') {
            return data
        } else {
            Notification.error({
                title: msg
            })
            return Promise.reject(msg)
        }
    }, err => {
        Notification.error({
            title: '网络请求超时',
            duration: 5000
            })
        
        return Promise.reject(err)
    })
    return instance(config)
}

export function post(url,data, headers) {
    return request({
        url,
        data,
        headers: {
            // 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
            // 'Content-Type': 'multipart/form-data',
            ...headers,
        },
        method: 'post'
    })
}

export function get(url,params,headers) {
    return request({
        url,
        params,
        headers
    })
}
